package com.zhika.zishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.MySelfQABean;
import com.zhika.zishi.beans.MySelfSPBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/16.
 * 这个是我的主页中我的关注和草稿的适配器
 */
public class MySelfLDAdapter extends BaseAdapter {

    private ArrayList<MySelfSPBean> ldArrayList;

    private Context context;

    public MySelfLDAdapter(Context context, ArrayList<MySelfSPBean> ldArrayList) {
        this.context = context;
        this.ldArrayList = ldArrayList;
    }

    @Override
    public Object getItem(int position) {
        return ldArrayList.get(position);
    }

    @Override
    public int getCount() {
        return ldArrayList.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MySelfSPBean bean = ldArrayList.get(position);
        String type = bean.getType();
        if (type.equals("strategy") || type.equals("problem") || type.equals("draft_strategy")) {
            View view = LayoutInflater.from(context).inflate(R.layout.myself_strategy_problem_layout, null);
            TextView typeText = (TextView) view.findViewById(R.id.myself_strategy_problem_type_text);
            typeText.setVisibility(View.VISIBLE);

            TextView titleText = (TextView) view.findViewById(R.id.myself_strategy_problem_title_text);

            ImageView arrowImageView = (ImageView) view.findViewById(R.id.myself_strategy_problem_arrow_image_view);

            TableRow commentRow = (TableRow) view.findViewById(R.id.myself_strategy_problem_comment_row);
            TextView commentText = (TextView) view.findViewById(R.id.myself_strategy_problem_comment_text);

            TableRow collectRow = (TableRow) view.findViewById(R.id.myself_strategy_problem_collect_row);
            TextView collectText = (TextView) view.findViewById(R.id.myself_strategy_problem_collect_text);

            TableRow likeRow = (TableRow) view.findViewById(R.id.myself_strategy_problem_like_row);
            TextView likeText = (TextView) view.findViewById(R.id.myself_strategy_problem_like_text);

            ImageButton imageButton = (ImageButton) view.findViewById(R.id.myself_strategy_problem_edit_image_button);
            imageButton.setVisibility(View.GONE);

            TableRow dateTimeRow = (TableRow) view.findViewById(R.id.myself_strategy_problem_date_time_row);
            dateTimeRow.setVisibility(View.VISIBLE);

            TextView dateText = (TextView) view.findViewById(R.id.myself_strategy_problem_date_text);

            TextView timeText = (TextView) view.findViewById(R.id.myself_strategy_problem_time_text);

            //草稿箱里的攻略
            if (type.equals("draft_strategy")) {
                typeText.setText("攻略：");
                arrowImageView.setVisibility(View.GONE);
                commentRow.setVisibility(View.GONE);
                collectRow.setVisibility(View.GONE);
                likeRow.setVisibility(View.GONE);

            }
            //我的关注中
            else {
                commentRow.setVisibility(View.VISIBLE);
                collectRow.setVisibility(View.VISIBLE);
                //攻略类型
                if (type.equals("strategy")) {
                    typeText.setText("攻略：");
                    arrowImageView.setVisibility(View.GONE);
                    likeRow.setVisibility(View.VISIBLE);
                    likeText.setText(bean.getLikeNumStr());
                }
                //问题类型
                else {
                    typeText.setText("问题：");
                    arrowImageView.setVisibility(View.VISIBLE);
                    likeRow.setVisibility(View.GONE);
                }
                titleText.setText(bean.getTitleNameStr());
                commentText.setText(bean.getCommentNumStr());
                collectText.setText(bean.getCollectNumStr());
            }

            dateText.setText(bean.getDateStr());

            timeText.setText(bean.getTimeStr());

            return view;
        }

        //问答类型或者是草稿箱里的问题
        else {
            View view = LayoutInflater.from(context).inflate(R.layout.myself_q_a_layout, null);
            TextView titleText = (TextView) view.findViewById(R.id.myself_q_a_title_text);

            ImageView arrowImageView = (ImageView) view.findViewById(R.id.myself_q_a_arrow_image_view);

            ListView listView = (ListView) view.findViewById(R.id.myself_q_a_list_view);

            titleText.setText(bean.getTitleNameStr());

            ArrayList<MySelfQABean> qaBeans = new ArrayList<>();

            MySelfQAAdapter adapter;

            //草稿箱里的问答
            if (type.equals("draft_qa")) {
                arrowImageView.setVisibility(View.GONE);

                qaBeans.clear();
                MySelfQABean bean1 = new MySelfQABean();
                bean1.setType("draft_qa");
                bean1.setAnswerContent("问题答案问题答案问题答案问题答案问题答案问题答案问题答案问题答案问题答案问题答案问题答案问题答案问题答案");
                bean1.setDateStr("4-09");
                bean1.setTimeStr("13:13");

                qaBeans.add(bean1);

                adapter = new MySelfQAAdapter(context,qaBeans);

                listView.setAdapter(adapter);
            }

            //我的关注里的问答
            else {
                qaBeans.clear();
                for (int i = 0; i < 3; i++) {
                    MySelfQABean bean1 = new MySelfQABean();
                    bean1.setType("qa");
                    bean1.setAnswerContent("问题的答案问题的答案问题的答案问题的答案问题的答案问题的答案问题的答案问题的答案问题的答案问题的答案");
                    bean1.setCommentNumStr("222");
                    bean1.setCollectNumStr("111");
                    bean1.setLikeNumStr("100");
                    bean1.setDateStr("4-14");
                    bean1.setTimeStr("12:45");

                    qaBeans.add(bean1);
                }

                adapter = new MySelfQAAdapter(context, qaBeans);
                listView.setAdapter(adapter);
            }




            return view;

        }
    }
}
